<template>
	<div class="role">
		<div class="container">
			<STform :ref-obj.sync="searchInfo.ref" :data="searchInfo.data"  :field-list="searchInfo.searchFormList" 
				:listTypeInfo="dictList" :inline="true" :rules="searchInfo.rules" label-width=" ">
				<template slot="form-btnSearch">
					<el-button type="primary" @click="doSearch">查询</el-button>
				</template>
				<template slot="form-btnOperation">
					<el-button type="primary" @click="onAdd">新增</el-button>
					<el-button type="warning" @click="onAlipay">财务</el-button>
					<el-button type="info" @click="onUser">超管</el-button>
				</template>
			</STform>
			<STtable size="mini" :data.sync="tableData" :field-list="tables" :checkBox="true" @doRefresh="fetchData" :pagination="pagination" :loading="loading" @handleEvent="handleEvent">
				<template slot="col-wallet" slot-scope="scope">{{(scope.row.wallet)? scope.row.wallet.realname+"/"+scope.row.wallet.alipay:"-"}}</template>
				<template slot="col-finance" slot-scope="scope">{{(scope.row.wallet)? scope.row.wallet.lstAmount+"/"+scope.row.wallet.dptAmount:"-"}}</template>
				<template slot="col-gender" slot-scope="scope">{{(dicts.sysUserSex[scope.row.gender])? dicts.sysUserSex[scope.row.gender]:"-"}}</template>
				<template slot="col-ouserid" slot-scope="scope">{{(scope.row.user)? scope.row.user.nick+"["+scope.row.ouserid+"]":"-"}}</template>
			</STtable>
		</div>
		<!-- 编辑弹出框 -->
		<el-dialog :title="page.popTitle" center :visible.sync="page.dialogVisible" :width="page.popWidth">
			<div v-if="page.pop == 1 || page.pop == 3" class="pop_box">
				<STform :ref-obj.sync="formInfo.ref" :data="formInfo.data" :field-list="formInfo.fieldList" :rules="formInfo.rules"
					 :listTypeInfo="dictList" label-width="100px"  @handleEvent="handleFormEvent"></STform>
				<div slot="footer" class="dialog-footer">
					<el-button @click="hidePop">取 消</el-button>
					<el-button type="primary" @click="doSubmit">确 定</el-button>
				</div>
			</div>
			<div v-else-if="page.pop == 2"  class="pop_box">
				<STform :ref-obj.sync="formInfo.ref" :data="formInfo.data" :field-list="formInfo.fieldUserList" :rules="formInfo.rulesUser"
					 :listTypeInfo="dictList" label-width="100px"  @handleEvent="handleFormEvent"></STform>
				<div slot="footer" class="dialog-footer">
					<el-button @click="hidePop">取 消</el-button>
					<el-button type="primary" @click="doSubmitUser">确 定</el-button>
				</div>
			</div>
			<div v-else-if="page.pop == 4"  class="pop_box">
				<STform :ref-obj.sync="formInfo.ref" :data="formInfo.data" :field-list="formInfo.fieldAlipayList" :rules="formInfo.rulesAlipay"
					 :listTypeInfo="dictList" label-width="100px"  @handleEvent="handleFormEvent"></STform>
				<div slot="footer" class="dialog-footer">
					<el-button @click="hidePop">取 消</el-button>
					<el-button type="primary" @click="doSubmitAlipay">确 定</el-button>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import STtable from '@/components/common/STtable'
	import STform from '@/components/common/STform'
	import starAPI from '@/api/app/ucenter/star.js'
	import {showToast} from '@/libs/core.js'
	import consts from './consts.js'
	import {mapState} from 'vuex' //本地取信息
	export default {
		components: {STtable,STform},
		data() {
			return {
				page: {
					popWidth: '85%',
					popTitle: null,
					dialogVisible: false,
					pop: 0
				},
				searchInfo: {
					ref: null,
					data: {
					},
					rules: {},
					searchFormList: consts.searchFormList // 查询列表表单列 数据
				},
				multipleSelection: [], //选中数据
				sysPageForm: { //搜索实际请求的字段
					name: '',
				},
				details: null, //单项数据
				loading: true,
				tableData: [], //列表数据
				tables:consts.tables,
				pagination: { // 分页数据
					pageNum: 1, // 当前页
					pageSize: 20, // 每页条数
					total: 0, //合计条数
					sorts: {id: 'DESC'},//排序信息
					pageSizes: [5, 10, 20, 50, 100], // 分页数量列表
					sizeChange: (val) => {
						this.onSizeChange(val)
					},
					currentChange: (pageNum) => {
						this.onCurrentChange(pageNum)
					},
					sortsChange:(sorts) => {
						this.onSortsChange(sorts);
					},
				},
				formInfo: { // 表格数据
					ref: null,
					data: null,
					rules: consts.rules,
					rulesUser: consts.rulesUser,
					rulesAlipay: consts.rulesAlipay,
					fieldList: consts.fieldList,
					fieldUserList: consts.fieldUserList,
					fieldAlipayList: consts.fieldAlipayList
				},
			}
		},
		async created() {
			await this.fetchData();
			this.loading = false;
		},
		computed: {
			dictList() {
				return this.$store.getters["dictSrv/list"];
			},
			...mapState({
        dicts: (state) => {
          return state.dictSrv.data;
        }
      })
		},
		methods: {
			doSearch() {
				const data = this.searchInfo.data;
				Object.assign(this.sysPageForm, data);
				this.fetchData(this.sysPageForm);
			},
			onAdd() {
				this.formInfo.ref && this.formInfo.ref.resetFields();
				this.formInfo.data = consts.fieldData(null);
				this.showPop(1);
			},
			async onEdit(data) {
				this.formInfo.ref && this.formInfo.ref.resetFields()
				data = JSON.parse(JSON.stringify(data))
				this.formInfo.data = consts.fieldData(data);
				this.showPop(3);
			},
			doSubmitAlipay() {
				this.formInfo.ref.validate(async (valid) => {
					if (valid) {
						const data = JSON.parse(JSON.stringify(this.formInfo.data));
						var res = await starAPI.doWallet(data);
						if (res) {
							await this.fetchData(null);
							this.hidePop();
						}
					} else {
						return false;
					}
				})
			},
			async onAlipay(data) {
				if (!this.multipleSelection || this.multipleSelection.length < 1) {
					showToast("请选中要操作的记录.");
					return;
				}
				let star = this.multipleSelection[0];
				this.formInfo.ref && this.formInfo.ref.resetFields();
				let wallet = (star.wallet)? star.wallet:null;
				this.formInfo.data = {realname:(wallet)? wallet.realname:'',
					 alipay:(wallet)? wallet.alipay:'', star:star.star, id:star.id};
				this.showPop(4);
			},
			async onUser() {
				if (!this.multipleSelection || this.multipleSelection.length < 1) {
					showToast("请选中要操作的记录.");
					return;
				}
				let star = this.multipleSelection[0];
				this.formInfo.ref && this.formInfo.ref.resetFields()
				this.formInfo.data = {ouserid:0, star:star.star, id:star.id};
				this.showPop(2);
			},
			async doSubmitUser() {
				this.formInfo.ref.validate(async (valid) => {
					if (valid) {
						const data = JSON.parse(JSON.stringify(this.formInfo.data));
						var res = await starAPI.doUser(data);
						if (res) {
							await this.fetchData(null);
							this.hidePop();
						}
					} else {
						return false;
					}
				})
			},
			async doSubmit() {
				this.formInfo.ref.validate(async (valid) => {
					if (valid) {
						const data = JSON.parse(JSON.stringify(this.formInfo.data));
						var res = await starAPI.doSave(data);
						if (res) {
							await this.fetchData(null);
							this.hidePop();
						}
					} else {
						return false;
					}
				})
			},
			onSizeChange(pageSize) {
				this.pagination.pageSize = pageSize
				this.fetchData(this.sysPageForm)
			},
			onCurrentChange(pageNum) {
				this.pagination.pageNum = pageNum
				this.fetchData(this.sysPageForm)
			},
			onSortsChange(sorts) {
				this.pagination.sorts = sorts;
				this.fetchData(this.sysPageForm);
			},
			async fetchData(params) {
				this.loading = false;
				const { pageNum, pageSize, sorts } = this.pagination;
				const ojson = await starAPI.getList({start:(pageNum - 1)*pageSize, sorts:sorts, limit:pageSize, ...params});
				if (ojson) {
					this.tableData = ojson.dataset;
					this.pagination.total = ojson.total;
				}
				this.loading = false;
				this.hidePop();
			},
			handleFormEvent(type, data) {
				switch (type) {
					case 'fileChange':
						if (data.value == 'avatar' && data.file) {
							this.formInfo.data.avatar = data.file.thumb;
						}
						break;
				}
			},
			handleEvent(type, data) {
				switch (type) {
					case 'tableCheck':
						this.multipleSelection = []
						for (const index in data) {
							this.multipleSelection.push(data[index]);
						}
						break;
					case 'tableDbClick':
						this.onEdit(data);
						break;
				}
			},
			showPop(pop) {
				if (pop == 1 || pop == 3) {
					this.page.popWidth = "620px";
					this.page.popTitle = (pop == 1)? '新增入驻':'编辑信息';
				} else if (pop == 2) {
					this.page.popWidth = "360px";
					this.page.popTitle = '设置超级管理员';
				} else if (pop == 4) {
					this.page.popWidth = "360px";
					this.page.popTitle = '设置收款支付宝';
				}
				this.page.pop = pop;
				this.page.dialogVisible = true;
			},
			hidePop() {
				this.page.dialogVisible = false;
				this.page.pop = 0;
			}
		}
	}
</script>
